<template>
	<view class="">
		<view class="s-input-wrap" style="margin:0">
			<view class="s-input-box flex-row">
				<text class="black-text">箱条码</text>
				<input v-model="form.sourceBoxBarcode" @confirm="setBarcode" class="s-input flex1" placeholder="扫描 / 输入箱条码" />
				<image class="s-input-icon" src="/static/images/scan.png"></image>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">物料编号</text>
				<text class="black-text font-30">{{form && form.productNo?form.productNo:'---'}}</text>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">物料名称</text>
				<text class="black-text font-30">{{form && form.productName?form.productName:'---'}}</text>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">原箱数量</text>
				<text class="black-text font-30">{{form && form.beforeBoxNum?form.beforeBoxNum:'---'}}</text>
			</view>
		</view>
		<view class="s-input-wrap" style="margin:25rpx 0 0">
			<view class="s-input-box flex-row">
				<text class="black-text">数量</text>
				<input type="number" v-model="form.afterBoxNum" @input="setNum" class="s-input flex1" placeholder="请输入数量" />
			</view>
		</view>
		<view class="content-list-total flex-row-between">
			<button @click="submit" class="botton p-botton botton-big">确定</button>
		</view>
	</view>
</template>

<script>
	import { validNum } from "@/utils/common"
	import {
		infoByBarcode,
		saveBarcode
	} from "@/api/common.js"
	import {
		unpack
	} from "./api.js"
	
	var printModule = uni.requireNativePlugin("pda-print-PdaPrintModule")
	
	export default {
		data(){
			return{
				form:{}
			}
		},
		methods:{
			setNum(e){
				this.$set(this.form,'afterBoxNum',validNum(e))
			},
			// 获取条码信息
			setBarcode(event){
				infoByBarcode(event.detail.value).then(res=>{
					if(res.code == 200){
						if(res.data.status != "NORMAL"){
							this.$modal.msg(`当前条码被锁定/冻结`);
							this.clearBarcode()
							return
						}
						// this.barCode = res.data.barcode
						// this.setCode()
						this.form = {
						  "afterBoxNum": 1, //res.data.qty
						  "beforeBoxNum": res.data.qty,
						  "productName": res.data.productName,
						  "productNo": res.data.productNo,
						  "sourceBoxBarcode": res.data.barcode,
						}
					}
				})
			},
			// 清空条码信息
			clearBarcode(){
				this.$set(this,'form',{})
			},
			// 拆箱
			submit(){
				if(!this.form.afterBoxNum){
					this.$modal.msg(`请输入拆箱数量`);
					return
				}
				if(this.form.afterBoxNum > this.form.beforeBoxNum){
					this.$modal.msg(`拆箱数量不能大于原箱数量`);
					return
				}
				this.$modal.confirm('是否确认拆箱并打印条码？').then(() => {
					this.$modal.loading("拆箱中...")
					saveBarcode({
						productNo: this.form.productNo,
						quantity: this.form.afterBoxNum,
					}).then(res=>{
						if(res.code == 200){
							this.form.newBarcode = res.data.barCode
							unpack(this.form).then(res=>{
								if(res.code == 200){
									this.$modal.closeLoading()
									this.$modal.msgSuccess("拆箱成功");
									/** 打印完成后才能清除form表单数据，避免打印空值 **/ 
									printModule.printLabelAsync({
										'barcode': res.data.barCode,
										'title': res.data.barCode + '\n\n物料编号：'+ this.form.productNo + '\n数量：'+this.form.afterBoxNum,
										'distance': 500,
									},(ret) => {
										console.log(ret);
									});
									this.clearBarcode()
									// 延迟1秒跳转
									setTimeout(()=>{
										uni.navigateBack();
									}, 1000);
								}
							})
						}
					})
				})
			},
		}
	}
</script>

<style>
</style>